//
//  ST_CloseFriendsCell.swift
//  btvideo
//
//  Created by Jason on 2021/9/6.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import PINRemoteImage
import DeviceKit





class ST_CloseFriendsCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .white
        
        [gradient_view,avatar_image_view,crown_image_view,nickname_label,value_label,nobility_image_view,grade_image_view].forEach({  contentView.addSubview($0) })
        
        
        gradient_view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        
        
        nickname_label.snp.makeConstraints { make in
            make.height.equalTo(25)
            make.width.lessThanOrEqualTo(ScreenWidth-90-30-50-19-6-30)
            make.left.equalToSuperview().offset(75)
            make.top.equalToSuperview().offset(16)
        }
        
        nobility_image_view.snp.makeConstraints { make in
            make.width.height.equalTo(19)
            make.left.equalTo(nickname_label.snp.right).offset(6)
            make.centerY.equalTo(nickname_label)
        }
        
        
        value_label.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.left.equalToSuperview().offset(75)
            make.top.equalToSuperview().offset(44)
        }
        
        grade_image_view.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
        }
        
        
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: -
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        v.cornerRadius = 50/2.0
        return v
    }()
    
    fileprivate lazy var crown_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        //v.cornerRadius = 5
        return v
    }()
    
    
    fileprivate lazy var nobility_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        //v.cornerRadius = 5
        return v
    }()
    
    fileprivate lazy var grade_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        //v.cornerRadius = 5
        return v
    }()
    
    
    fileprivate lazy var nickname_label:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        l.font = .systemFont(ofSize: 18, weight: .semibold)
//        l.numberOfLines = 0
       // l.backgroundColor = .random
        return l
    }()
    
    fileprivate lazy var value_label:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.6745098039, green: 0.7058823529, blue: 0.7529411765, alpha: 1)
        l.font = .systemFont(ofSize: 14, weight: .regular)
//        l.numberOfLines = 0
        return l
    }()
    
    fileprivate lazy var gradient_view:UIView = {
        let l = UIView()
        l.cornerRadius = 12
        return l
    }()
    
    var model:ST_GiftRankCellModel?{
        didSet{
            
            guard let m = model else { return }
            
            avatar_image_view.pin_setImage(from: m.avatar.compress_small_image(width: 34*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            
            crown_image_view.image = m.crownImage
            
            nickname_label.text = m.nickname
            
            value_label.text = "亲密度：" + m.intimacy.string
            
            if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male{
                
                nobility_image_view.image = nil
                
            }else{
                
                nobility_image_view.pin_setImage(from: m.nobilityUrl?.compress_small_image(width: 34*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
                
            }
            
            
            grade_image_view.pin_setImage(from: m.intimacyImg.compress_small_image(width: 34*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            switch m.index {
            case 0:
                gradient_view.backgroundColor = #colorLiteral(red: 1, green: 0.768627451, blue: 0.7490196078, alpha: 0.2)
            case 1:
                gradient_view.backgroundColor = #colorLiteral(red: 1, green: 0.8823529412, blue: 0.5803921569, alpha: 0.2)
            case 2:
                gradient_view.backgroundColor = #colorLiteral(red: 0.737254902, green: 0.8549019608, blue: 1, alpha: 0.2)
            default:
                gradient_view.backgroundColor = .clear
            }
        }
    }
}



class CloseFriendsNormalCell: ST_CloseFriendsCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        avatar_image_view.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(16)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class CloseFriendsCrownCell: ST_CloseFriendsCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        avatar_image_view.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(23)
        }


        crown_image_view.snp.makeConstraints { make in
            make.centerX.equalTo(avatar_image_view)
            make.top.equalToSuperview().offset(7)
            make.width.equalTo(52)
            make.height.equalTo(67)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}





#if PREVIEW
import SwiftUI

struct TIANPreviewProvider: PreviewProvider {
  static var previews: some View {
    Group{
        
        DebugPreviewView {
      
            let view = CloseFriendsCrownCell()
            
            view.model = .create_test_case()
            
            return view
        }
        .frame(width: 375, height: 80.0)
    }
        
  }
}
#endif
